package com.letv.core.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Proxy;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.letv.core.constant.NetworkConstant;
import com.letv.core.db.PreferencesManager;

/**
 * 网络相关帮助类
 * 
 * @author zhuqiao
 *
 */
/**
 * 
 * @author 吴新荣
 * 重构
 */
public class NetworkUtils {
    /**
     * 手机厂商名字
     * 1:移动
     * 2:联通
     * 3:电信
     * */
    public static int ProvidersName = -1;
    public static final int OPERATOR_UNKNOWN        = 0;
    public static final int OPERATOR_CHINA_MOBILE   = 1;
    public static final int OPERATOR_CHINA_UNICOM   = 2;
    public static final int OPERATOR_CHINA_TELECOM  = 3;
    

//    public static boolean isMobileNetWork() {
//        boolean isMobile = false;
//        if (isNetworkAvailable()) {
//            if (!isWifi()) {
//                isMobile = true;
//            }
//        }
//
//        return isMobile;
//    }

    // 用来检测当前网络是否可用:true可用；false不可用
//    public static boolean isNetworkAvailable() {
//        return getAvailableNetworkInfo() != null;
//    }

//    public static boolean isMobileNetwork() {
//        // 网络不可用，返回false
//        NetworkInfo ni = getAvailableNetworkInfo();
//        if (null == ni) {
//            return false;
//        }
//
//        if (ni.getType() == ConnectivityManager.TYPE_WIFI) {
//            return false;
//        }
//
//        return true;
//    }

    /**
     * 判断是否是3G网络，包括移动、联通、电信3G
     * 4G视为3G处理
     * @return
     */
//    public static boolean is3GNetwork() {
//        boolean result = true;
//        TelephonyManager telephonyManager = (TelephonyManager) BaseApplication.getInstance().getSystemService(
//                Context.TELEPHONY_SERVICE);
//        int type = telephonyManager.getNetworkType();
//        switch (type) {
//            case TelephonyManager.NETWORK_TYPE_GPRS:
//            case TelephonyManager.NETWORK_TYPE_CDMA:
//            case TelephonyManager.NETWORK_TYPE_EDGE:
//                result = false;
//                break;
//            default:
//                break;
//        }
//        return result;
//    }

//    public static NetworkInfo getAvailableNetworkInfo() {
//        NetworkInfo ni = null;
//        try {
//            ConnectivityManager cm = (ConnectivityManager) BaseApplication.getInstance().getSystemService(
//                    Context.CONNECTIVITY_SERVICE);
//            ni = cm.getActiveNetworkInfo();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        return (ni != null && ni.isAvailable() ? ni : null);
//    }

    public static NetworkInfo getAvailableNetworkInfo(Context context) {
        NetworkInfo ni = null;
        try {
            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            ni = cm.getActiveNetworkInfo();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (ni != null && ni.isAvailable() ? ni : null);
    }

//    public static int getNetworkType() {
//        int type = NetworkConstant.NETWORK_TYPE_NO_NET;
//
//        ConnectivityManager connectivityManager = (ConnectivityManager) BaseApplication.getInstance().getSystemService(
//                Context.CONNECTIVITY_SERVICE);
//        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
//
//        if (networkInfo != null && networkInfo.isAvailable()) {
//            if (ConnectivityManager.TYPE_WIFI == networkInfo.getType()) {
//                return NetworkConstant.NETWORK_TYPE_WIFI;
//            } else {
//                TelephonyManager telephonyManager = (TelephonyManager) BaseApplication.getInstance().getSystemService(
//                        Context.TELEPHONY_SERVICE);
//
//                switch (telephonyManager.getNetworkType()) {
//                    case TelephonyManager.NETWORK_TYPE_GPRS:
//                    case TelephonyManager.NETWORK_TYPE_CDMA:
//                    case TelephonyManager.NETWORK_TYPE_EDGE:
//                        type = NetworkConstant.NETWORK_TYPE_2G;
//                        break;
//                    case TelephonyManager.NETWORK_TYPE_LTE:
//                        type = NetworkConstant.NETWORK_TYPE_3G; // 4G作为3G处理
//                        break;
//                    default:
//                        type = NetworkConstant.NETWORK_TYPE_3G;
//                        break;
//                }
//            }
//        }
//        return type;
//    }

    public static int getNetworkType(Context context) {
        int type = NetworkConstant.NETWORK_TYPE_NO_NET;

        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(
            Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

        if (networkInfo != null && networkInfo.isAvailable()) {
            if (ConnectivityManager.TYPE_WIFI == networkInfo.getType()) {
                return NetworkConstant.NETWORK_TYPE_WIFI;
            } else {
                TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(
                    Context.TELEPHONY_SERVICE);

                switch (telephonyManager.getNetworkType()) {
                    case TelephonyManager.NETWORK_TYPE_GPRS:
                    case TelephonyManager.NETWORK_TYPE_CDMA:
                    case TelephonyManager.NETWORK_TYPE_EDGE:
                        type = NetworkConstant.NETWORK_TYPE_2G;
                        break;
                    case TelephonyManager.NETWORK_TYPE_LTE:
                        type = NetworkConstant.NETWORK_TYPE_3G; // 4G作为3G处理
                        break;
                    default:
                        type = NetworkConstant.NETWORK_TYPE_3G;
                        break;
                }
            }
        }
        return type;
    }


    /**
     * 根据网络号判断运营商名称
     * 0-未知 1-中国移动 2-中国联通 3-中国电信
     * @return
     */
//    private static int getOperatorByNetworkNumber(final String number) {
//        int result = OPERATOR_UNKNOWN;
//        if (!TextUtils.isEmpty(number)) {
//            if (number.startsWith(NetworkConstant.NETWORK_NUMBER_46000)
//                    || number.startsWith(NetworkConstant.NETWORK_NUMBER_46002)
//                    || number.startsWith(NetworkConstant.NETWORK_NUMBER_46007)) {
//                result = OPERATOR_CHINA_MOBILE;
//            } else if (number.startsWith(NetworkConstant.NETWORK_NUMBER_46001)
//                    || number.startsWith(NetworkConstant.NETWORK_NUMBER_46010)) {
//                result = OPERATOR_CHINA_UNICOM;
//            } else if (number.startsWith(NetworkConstant.NETWORK_NUMBER_46003)) {
//                result = OPERATOR_CHINA_TELECOM;
//            }
//        }
//
//        return result;
//    }
    /**
     * 是否为联通3G
     * */
//    public static boolean isUnicom3G(boolean isFirst) {
//        ConnectivityManager connectivityManager = (ConnectivityManager) BaseApplication.getInstance().getSystemService(
//                Context.CONNECTIVITY_SERVICE);
//        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
//
//        if (networkInfo != null && networkInfo.isAvailable()) {
//            if (ConnectivityManager.TYPE_WIFI == networkInfo.getType()) {
//                return false;
//            }
//            TelephonyManager tm = (TelephonyManager) BaseApplication.getInstance().getSystemService(
//                    Context.TELEPHONY_SERVICE);
//            String networkNumber = tm.getSimOperator();
//            if (TextUtils.isEmpty(networkNumber)) {
//                networkNumber = tm.getSubscriberId();
//            }
//
//            if (networkNumber != null) {
//                int operator = getOperatorByNetworkNumber(networkNumber);
//                // 中国联通网络号
//                if (OPERATOR_CHINA_UNICOM == operator) {
//                    String netTypeName = networkInfo.getExtraInfo();
//                    if ("3gwap".equals(netTypeName) || "uniwap".equals(netTypeName) || "3gnet".equals(netTypeName)
//                            || "uninet".equals(netTypeName)) { // 联通
//                        if ("3gnet".equals(netTypeName) || "uninet".equals(netTypeName)) {
//                            if (isFirst) {
//                                return true;
//                            } else {
//                                return PreferencesManager.getInstance().isChinaUnicomSwitch();
//                            }
//                        }
//                        return false;
//                    } else if ("cmnet".equals(netTypeName) || "cmwap".equals(netTypeName)) { // 移动
//                        return false;
//                    } else if ("ctnet".equals(netTypeName) || "ctwap".equals(netTypeName)) { // 电信
//                        return false;
//                    }
//                }
//
//            } else {
//                return false;
//            }
//        }
//        return false;
//    }

//    public static boolean isWifi() {
//        boolean result = false;
//        NetworkInfo networkInfo = getAvailableNetworkInfo();
//        if (networkInfo != null) {
//            if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
//                result = true;
//            }
//        }
//        return result;
//    }

    public static boolean isWifi(Context context) {
        boolean result = false;
        NetworkInfo networkInfo = getAvailableNetworkInfo(context);
        if (networkInfo != null) {
            if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                result = true;
            }
        }
        return result;
    }

    /**
     * 网络是否可用
     * 
     * @return
     */
    public static boolean isNetworkAvailable(Context context) {
        NetworkInfo ni = null;
        try {
            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            ni = cm.getActiveNetworkInfo();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return (ni != null && ni.isAvailable());
    }

    /**
     * 网络是否连接
     * 
     * @param context
     * @return
     */
//    public static boolean isNetworkConnected(Context context) {
//        boolean isConnected = false;
//        try {
//            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//            NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
//            isConnected = activeNetwork.isConnectedOrConnecting();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return isConnected;
//    }

    /**
     * 判断网络是否是设置了代理
     */
//    @SuppressWarnings("deprecation")
//    public static boolean isProxy() {
//        return !TextUtils.isEmpty(Proxy.getDefaultHost());
//    }

    /**
     * 检测是否由代理连接
     * 
     * @param context
     * @return
     */
    @SuppressWarnings("deprecation")
    public static boolean checkIsProxyNet(Context context) {
        final WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        manager.getConnectionInfo();
        String proxyAddress;
        // android sdk版本大于ics
        final boolean isAboveICS = Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;
        if (isAboveICS) {
            proxyAddress = System.getProperty("http.proxyHost");
        } else {
            proxyAddress = android.net.Proxy.getHost(context);
        }

        return !TextUtils.isEmpty(proxyAddress);
    }

    public static String netTypeToString(int netType) {
        switch (netType) {
            case NetworkConstant.NETWORK_TYPE_NO_NET:
                return "none";
            case NetworkConstant.NETWORK_TYPE_4G:
                return "3G";    // 4G作为3G处理
            case NetworkConstant.NETWORK_TYPE_3G:
                return "3G";
            case NetworkConstant.NETWORK_TYPE_2G:
                return "2G";
            case NetworkConstant.NETWORK_TYPE_WIFI:
                return "wifi";
            default:
                return "unknown";
        }
    }

    /**
     * 判断为哪个运营商
     */
//    public static int getOperator(Context context) {
//        ConnectivityManager connectivityManager = (ConnectivityManager) context
//                .getSystemService(Context.CONNECTIVITY_SERVICE);
//        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
//        String imsi = "";
//        if (networkInfo != null && networkInfo.isAvailable()) {
//            TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
//            imsi = tm.getSimOperator();
//            if (TextUtils.isEmpty(imsi)) {
//                imsi = tm.getSubscriberId();
//            }
//
//        }
//        if (!TextUtils.isEmpty(imsi)) {
//            return getOperatorByNetworkNumber(imsi);
//        }
//        return OPERATOR_UNKNOWN;
//    }

    /**
     * 获取是否是联通网络
     * 
     * @return
     */
//    public static boolean getNetTypeForWo() {
//        return getNetTypeForWo(false);
//    }

    /**
     * 是否为联通3G
     * */
//    public static boolean getNetTypeForWo(boolean isFirst) {
//        ConnectivityManager connectivityManager = (ConnectivityManager) BaseApplication.getInstance().getSystemService(
//                Context.CONNECTIVITY_SERVICE);
//        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
//
//        if (networkInfo != null && networkInfo.isAvailable()) {
//            if (ConnectivityManager.TYPE_WIFI == networkInfo.getType()) {
//                return false;
//            }
//            TelephonyManager telephonyManager = (TelephonyManager) BaseApplication.getInstance().getSystemService(
//                    Context.TELEPHONY_SERVICE);
//            String operator = telephonyManager.getSimOperator();
//            if (operator == null || operator.equals("")) {
//                operator = telephonyManager.getSubscriberId();
//            }
//            if (operator != null) {
//                // LogInfo.log("king","operator = " + operator);
//                if (operator.startsWith("46000") || operator.startsWith("46002")) {
//                    ProvidersName = 1;
//                } else if (operator.startsWith("46001")) {
//                    ProvidersName = 2;
//                } else if (operator.startsWith("46003")) {
//                    ProvidersName = 3;
//                }
//
//                if (ProvidersName == 2) {
//                    String netTypeName = networkInfo.getExtraInfo();
//                    // LogInfo.log("king","netTypeName = " + netTypeName);
//                    if ("3gwap".equals(netTypeName) || "uniwap".equals(netTypeName) || "3gnet".equals(netTypeName)
//                            || "uninet".equals(netTypeName)) { // 联通
//                        if ("3gnet".equals(netTypeName) || "uninet".equals(netTypeName)) {
//                            if (isFirst) {
//                                return true;
//                            } else {
//                                return PreferencesManager.getInstance().isChinaUnicomSwitch();
//                            }
//                        }
//                        return false;
//                    } else if ("cmnet".equals(netTypeName) || "cmwap".equals(netTypeName)) { // 移动
//                        return false;
//                    } else if ("ctnet".equals(netTypeName) || "ctwap".equals(netTypeName)) { // 电信
//                        return false;
//                    }
//                }
//
//            } else {
//                return false;
//            }
//        }
//        return false;
//    }

    // /**
    // * 网络是否处于联通状态，用于播放
    // */
    // public static boolean isNetAvailableForPlay(final Context context) {
    // NetworkInfo networkInfo = null;
    // try {
    // networkInfo = getAvailableNetworkInfo();
    // } catch (Exception e) {
    // e.printStackTrace();
    // }
    //
    // return networkInfo != null;
    // }

    /**
     * 是否是2G 或3G网络
     *
     * @return
     */
//    public static boolean is2GOr3G() {
//        return (NetworkUtils.getNetworkType() == NetworkConstant.NETWORK_TYPE_2G || NetworkUtils.getNetworkType() == NetworkConstant.NETWORK_TYPE_3G);
//    }
}
